home *** CD-ROM | disk | FTP | other *** search
- package symantec.itools.db.net;
-
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.Vector;
- import symjava.sql.SQLException;
-
- class MPlex {
- Vector _IOs;
- InetAddress _host;
- int _port;
- boolean _shareSocket;
- int _connectionID;
- ClientSession _sess;
- boolean _connLost;
-
- MPlex(InetAddress host, int port, boolean shareSocket, ClientSession sess) {
- this._host = host;
- this._port = port;
- this._shareSocket = shareSocket;
- this._sess = sess;
- this._IOs = new Vector();
- this._connLost = false;
- }
-
- void connect() throws NetException, ErrorException {
- SessionRequest s = new SessionRequest(this._sess);
- this._connectionID = s.connect();
- }
-
- void disconnect() {
- try {
- SessionRequest s = new SessionRequest(this._sess);
- s.disconnect();
- } catch (Exception var2) {
- }
- }
-
- short getNextID() throws NetException {
- for(short i = 0; i < this._IOs.size(); ++i) {
- if ((IO)this._IOs.elementAt(i) == null) {
- return i;
- }
- }
-
- if (this._IOs.size() < 32765) {
- return (short)this._IOs.size();
- } else {
- throw new NetException("IO object limit reached");
- }
- }
-
- Socket getSocket() throws NetException {
- Socket s = null;
- if (this._shareSocket && this._IOs.size() != 0) {
- IO io = (IO)this._IOs.elementAt(0);
- s = io._sock;
- } else {
- if (this._port < 80) {
- throw new NetException("Port# must be 80 or higher");
- }
-
- try {
- s = new Socket(this._host, this._port);
- } catch (UnknownHostException var3) {
- throw new NetException("Unknown host");
- } catch (IOException var4) {
- throw new NetException("Server not responding");
- } catch (SecurityException var5) {
- throw new NetException("Security exception");
- }
- }
-
- return s;
- }
-
- synchronized IO getIO() throws NetException, SQLException {
- if (this._connLost) {
- throw new SQLServerConnException();
- } else {
- short id = this.getNextID();
- Socket s = this.getSocket();
- IO io;
- if (this._shareSocket && this._IOs.size() != 0) {
- IO sysIO = (IO)this._IOs.elementAt(0);
- io = new IO(sysIO._writer, sysIO.getInputStream(), id);
- } else {
- io = new IO(this, s, id);
- }
-
- if (id == this._IOs.size()) {
- this._IOs.addElement(io);
- } else {
- this._IOs.setElementAt(io, id);
- }
-
- if (id != 0 && !this._shareSocket) {
- try {
- SessionRequest sess = new SessionRequest(this._sess);
- sess.reconnect(io, this._connectionID);
- } catch (ErrorException var5) {
- throw new NetException("Error reconnecting IO object");
- }
- }
-
- return io;
- }
- }
-
- synchronized void close() {
- for(short i = 0; i < this._IOs.size(); ++i) {
- if ((IO)this._IOs.elementAt(i) != null) {
- ((IO)this._IOs.elementAt(i)).close();
- }
- }
-
- this._connLost = true;
- }
-
- synchronized void releaseIO(IO io) {
- if (io._id != 0) {
- io.close();
- this._IOs.setElementAt((Object)null, io._id);
- }
-
- }
-
- synchronized OutputStream getStream(short streamID) {
- IO io = (IO)this._IOs.elementAt(streamID);
- return io._readerOS;
- }
-
- synchronized void connectionLost() {
- this._connLost = true;
- this._sess.lostConnection();
- }
-
- synchronized void connectionLost(SocketReader r) {
- SocketWriter wtr = null;
-
- for(short i = 0; i < this._IOs.size(); ++i) {
- IO io = (IO)this._IOs.elementAt(i);
- if (io != null && io._reader == r) {
- wtr = io._writer;
- break;
- }
- }
-
- if (wtr != null) {
- for(int i = 0; i < this._IOs.size(); ++i) {
- IO io = (IO)this._IOs.elementAt(i);
- if (io != null && io._writer == wtr) {
- NetPipedInputStream str = (NetPipedInputStream)io._userIS;
- str.connBroken();
- }
- }
-
- }
- }
- }
-